package controller.employee;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import view.Home;
import view.Login;
import dao.EmployeeDAO;

public class CheckLogin implements ActionListener {
	private EmployeeDAO empDAO;
	private Login login;

	public CheckLogin(Login login) {
		this.login = login;
		empDAO = new EmployeeDAO();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == login.getBtnLogin()) {
			doLogin(e);
		}
	}

	public void doLogin(ActionEvent event) {
		String user = login.getTfAccount().getText();
		String pass = new String(login.getTfPass().getPassword());
		
		if (empDAO.checkAccount(user, pass)) {
			login.setVisible(false);
			Home.getInstance();//new doi tuong home
			if(user.equals("admin")) Home.getBtnMainEmployee().setVisible(true);
			else Home.getBtnMainEmployee().setVisible(false);
			Home.getLblMainUser().setText("Welcome:  "+Home.getEmployee().getName());
			Home.getInstance().setLocationRelativeTo(null);
			Home.getInstance().setVisible(true);
		} else {
			JOptionPane.showMessageDialog(null, "Login Fail");
		}

	}

}
